# this is due to the difference of nil and false
#in sql screwing my querys
def normalize_boolean_fields
	[:shirt,:tutor,:paid,:lodge,:queue,:canceled].each do |e|
		all = Attendee.find(:all, :conditions => {e => nil})
		all.each {|a| a.update_attribute(e,false); a.save(false)}
	end
end

def normalize_attendee_names
	attendees = Attendee.find(:all)
	
	attendees.each do |a|
		names = a.name.split(" ")
		names.map { |n| n.capitalize! }
		a.name = names.join(" ")
		a.save
	end
end

def change_password password,options = {}
    a = User.find(:first, :conditions => options)
    a.password = password
    a.password_confirmation = password
    a.save
end

def give_lodge_to_people_on_queue
	how_much = Attendee.max_lodge_size - Attendee.lodge_total

	who = Attendee.find(:all, :conditions => {:queue => true, :paid => true}, :limit => how_much)
	who.each do |w|
		puts "Gave lodge to #{w.name}"
		w.queue = false
		w.lodge = true
		w.save(false)
	end
end

def take_lodge_from_unpaid
	who = Attendee.find(:all, :conditions => {:lodge => true, :paid => false})
	who.each do |w|
		puts "Destroyed #{w.name}"
		w.lodge = false
		w.save(false)
	end
end

def ies_all
    ies = Attendee.find(:all).map {|a| a.ies }.uniq

    all = ies.map { |i| [ i,Attendee.find(:all,:conditions => ["UPPER(ies) = ?", i.upcase] ) ] }

    File.open("ies.txt","w") do |f|
        all.each do |i|
            f.puts "#{i[0]}" #nome da ies
            i[1].each { |a| f.puts "#{a.name},PET #{a.pet},#{a.get_email}" }
            f.puts ""
        end
    end

end

def ies_single
	all = (Attendee.find(:all, :conditions => { :paid => true, :lodge => true }) - Attendee.find(:all, :conditions => {:city => 'Porto Alegre' }) ).map { |a| "#{a.name},#{a.pet},#{a.ies}"}

	File.open("ies2.txt","w") { |f| f.puts("Nome,PET,IES");f.puts(all); }
end

def ies_paid
	all = (Attendee.find(:all, :conditions => { :paid => true }) - Attendee.find(:all, :conditions => {:city => 'Porto Alegre' }) ).map { |a| "#{a.name},#{a.pet},#{a.ies}"}

	File.open("ies2.txt","w") { |f| f.puts("Nome,PET,IES");f.puts(all); }
end

def send_pre_inscricao
    #send a message to all who havent paid
    who = Attendee.find(:all)
    who.each do |w|
        puts "Mailed #{w.name}"
        title = "Pré-inscrição no SulPET2010"
        message = Emailer.pre_inscricao
        Emailer.deliver_contact(w.get_email,title,message)
    end
end

def send_alojamento_fila
    who = Attendee.find(:all)
    who.each do |w|
        puts "Mailed #{w.name}"
        title = "Fila para o Alojamento"
        message = Emailer.fila
        Emailer.deliver_contact(w.get_email,title,message)
    end
end

#def send_confirma_alojamento
#    who = Attendee.find(:all, :conditions => {:lodge => true, :paid => false} )
#    who.each do |w|
#        puts "Mailed #{w.name}"
#        title = "Confirme o seu alojamento."
#        message = Emailer.alojamento
#        Emailer.deliver_contact(w.get_email,title,message)
#    end
#end

def send_fim
    who = Attendee.find(:all)
    who.each do |w|
        puts "Mailed #{w.name}"
        title = "Fim das Inscrições 26/05"
        message = Emailer.fim_inscricao
        Emailer.deliver_contact(w.get_email,title,message)
    end
end

def send_eleicao_mim
    who = Attendee.find(:all,:conditions => {:user_login => 'fialho'} )
    who.each do |w|
        puts "Mailed #{w.name}"
        title = "Eleições Cenapet"
        message = Emailer.eleicao
        Emailer.deliver_contact(w.get_email,title,message)
    end
end

def send_eleicao
    who = Attendee.find(:all)
    who.each do |w|
        puts "Mailed #{w.name}"
        title = "Eleições Cenapet"
        message = Emailer.eleicao
        Emailer.deliver_contact(w.get_email,title,message)
    end
end

def send_lodge_confirm
	  who = Attendee.find(:all, :conditions => {:lodge => true} )
    who.each do |w|
        puts "Mailed #{w.name}"
        title = "Confirmação do Alojamento"
        message = Emailer.lodge_confirm
        Emailer.deliver_contact(w.get_email,title,message)
    end
end

def send_cancel
		who = Attendee.find(:all, :conditions => {:paid => false} )
    who.each do |w|
        puts "Mailed #{w.name}"
        title = "Não existe possibilidade de inscrição na hora do evento"
        message = Emailer.cancel
        Emailer.deliver_cancel(w.get_email,title,message)
    end
end
